//
//  KingView.m
//  SZBank
//
//  Created by rimi on 16/7/11.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "KingView.h"
#import "TableImageView.h"
#import "BJSeriesView.h"
#import "BJCompareImageView.h"
@interface KingView ()<ButtonAndTableViewDelegate>
{
    NSMutableArray *hotSellArray;/**< 热销产品 */
    NSArray *orginDataArray;/**< 原始数据 */
    NSArray *INTOArray;/**< 简介数组 */
    NSMutableArray *array_ID;/**< 选项ID */
    NSMutableArray *compareDataArray;/**< 对比数据源数组 */
}
@property (nonatomic)ButtonAndTableView *HotProductsButton;/**< 热销产品 */
@property (nonatomic)UIButton *checkButton;/**< 查看对比按钮 */
@end

@implementation KingView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        

        
        
        //初始化数组
        hotSellArray = [NSMutableArray array];
        INTOArray = [NSArray array];
        array_ID = [NSMutableArray array];
        compareDataArray = [NSMutableArray array];
        //网络请求(产品分类)
        NSDictionary *paraDic = @{@"time":@"2013-01-01 12:40:10"};
        
        [NetWorkingDataManager  getProtocolGoldPomegranateTypeWithParam:paraDic success:^(id sender) {
            //数据整理
            NSArray *array = sender[@"datalist"];
            INTOArray = array;
//            NSLog(@"%@",array);
            for (NSDictionary *dict in array) {
                NSString *string = dict[@"NAME"];
                if (![hotSellArray containsObject:string]) {
                    [hotSellArray addObject:string];
                    [array_ID addObject:dict[@"ID"]];
                }
                
            }
            //添加下拉button
            [hotSellArray removeLastObject];
            [hotSellArray removeLastObject];
            [array_ID removeLastObject];
            [array_ID removeLastObject];
            self.HotSellDic = @{@"title":@"热销产品", @"list":hotSellArray};
            [self addSubview:self.HotProductsButton];
             [self addSubview:self.checkButton];
            //
           
            
        } failure:^(id sender) {
            NSLog(@"%@",sender);
        }];
        //网络请求（本地数据库更新）
        [NetWorkingDataManager getProtocolGoldPomegranateWithParam:paraDic success:^(id sender) {
             NSArray *array = sender[@"datalist"];
            orginDataArray = [NSArray array];
            orginDataArray = array ;
            [self NetDataHandllWithArray:array];
            
            
//            NSLog(@"%@",sender);
            
        } failure:^(id sender) {
            
        }];
       
        

    }
    return self;
}



#pragma mark -- ButtonAndTableViewDelegate
-  (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    //首先移除对比数组中的所有元素
    [compareDataArray removeAllObjects];
    
    
     NSMutableArray *dataMutableArray = [NSMutableArray array];
    for (int i = 0; i < orginDataArray.count; i ++) {
        if ([orginDataArray[i][@"CATEGORY"] isEqualToString:array_ID[indexPath.row] ]) {
            [dataMutableArray addObject:orginDataArray[i]];
           
        }
    }
    
    if (!(indexPath.row > 1 && indexPath.row < 5)) {
       
        //关闭滑动
        self.scrollView.scrollEnabled = NO;
        //移除以前的视图
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        //添加视图
        BJSeriesView *webView = [[BJSeriesView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) withDataString:INTOArray[indexPath.row][@"INTO"] tableImageArray:dataMutableArray];
        
        webView.bjSeriesViewBlock = ^(NSDictionary *dictionary){
            if (compareDataArray.count == 0) {
                [compareDataArray addObject:dictionary];
            }else if (compareDataArray.count == 1){
                if ([compareDataArray containsObject:dictionary]) {
                    [compareDataArray removeAllObjects];
                }else{
                    [compareDataArray addObject:dictionary];
                }
            }else{
                for (int i = 0; i < compareDataArray.count; i ++) {
                    if ([compareDataArray[i][@"dataSource"][0] isEqualToString:dictionary[@"dataSource"][0] ]) {
                        [compareDataArray removeObjectAtIndex:i];
                    }else{
                         [compareDataArray addObject:dictionary];
                    }
                }
            }
            
            
            
        };
       
        
        [self.scrollView addSubview:webView];
    }else{
          [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }



    
}

#pragma mark -- actions
- (void)cheakButtonClick:(UIButton *)sender{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_notification) name:@"BJnotification" object:nil];
    
    BJCompareImageView *compareImageView = [[BJCompareImageView alloc]initWithFrame:self.window.frame withDataArray:compareDataArray];
    
    
    
   
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:compareImageView];
}

- (void)action_notification{
    [compareDataArray removeAllObjects];
}

#pragma mark -- NetDataHandll
- (void)NetDataHandllWithArray:(NSArray *)dataArray{
    NSMutableDictionary *tableDict = [NSMutableDictionary dictionary];
    NSMutableArray *NameArray = [NSMutableArray array];

    for (NSDictionary *dict in dataArray) {
        NSString *titleString = dict[@"NAME"];
       
       
        
        NSString *investmemDayString = [NSString stringWithFormat:@"%@天",dict[@"INVESTMENTPERIOD"]];
        //预期年收益
        NSString *investmemString;
        if (![dict[@"YIELDLOW"] isEqualToString:dict[@"YIELDUP"]]) {
            investmemString  = [NSString stringWithFormat:@"%@%@~%@%@",dict[@"YIELDLOW"],@"%",dict[@"YIELDUP"],@"%"];
        }else{
            investmemString = [NSString stringWithFormat:@"%@%@",dict[@"YIELDUP"],@"%"];
        }
        //
        NSString *INVESTMENTTHRESHOLD = [NSString stringWithFormat:@"%@万",dict[@"INVESTMENTTHRESHOLD"]] ;
        NSString *SUBSTARTTIME = [dict[@"SUBSTARTTIME"] substringToIndex:10];
        NSString *SUBENDTIME = [dict[@"SUBENDTIME"] substringToIndex:10];;
        NSString *INTERESTSTARTDATE = [dict[@"INTERESTSTARTDATE"] substringToIndex:10];
        NSString *INTERESTENDDATE = [dict[@"INTERESTENDDATE"] substringToIndex:10];
        
        //加入数组
        NSArray *array1 = @[titleString,investmemDayString,investmemString,INVESTMENTTHRESHOLD,SUBSTARTTIME,SUBENDTIME,INTERESTSTARTDATE,INTERESTENDDATE];
        
        //加入字典
        [tableDict setObject:array1 forKey:dict[@"NAME"]];

  
       [NameArray addObject:[NSString stringWithFormat:@"%@", dict[@"CATEGORY"]] ];
    
        
    }
    
 
    
    //默认表格式图
    TableImageView *Glodview1 = [[TableImageView alloc]initWithFrame:CGRectMake(frameWidth* 0.04,frameHeight * 0.04, frameWidth * 0.3  , frameHeight * 0.55) withDataDictionary:@{@"labels":tableDict[@"“金石榴创盈130903M1号”人民币理财产品"]} tableStyle:0];
    Glodview1.tableViewBlock = ^(NSDictionary *dictionary){
        if (compareDataArray.count == 0) {
            [compareDataArray addObject:dictionary];
        }else if (compareDataArray.count == 1){
            if ([compareDataArray containsObject:dictionary]) {
                [compareDataArray removeAllObjects];
            }else{
                [compareDataArray addObject:dictionary];
            }
        }else{
            for (int i = 0; i < compareDataArray.count; i ++) {
                if ([compareDataArray[i][@"dataSource"][0] isEqualToString:dictionary[@"dataSource"][0] ]) {
                    [compareDataArray removeObjectAtIndex:i];
                }
            }
        }

    };

    
    
    
    TableImageView *Glodview2 = [[TableImageView alloc]initWithFrame:CGRectMake(frameWidth* 0.4,frameHeight * 0.04, frameWidth * 0.3  , frameHeight * 0.55) withDataDictionary:@{@"labels":tableDict[@"“金石榴惠盈130903M2号”人民币理财产品"]} tableStyle:0];
    Glodview2.tableViewBlock = ^(NSDictionary *dictionary){
        if (compareDataArray.count == 0) {
            [compareDataArray addObject:dictionary];
        }else if (compareDataArray.count == 1){
            if ([compareDataArray containsObject:dictionary]) {
                [compareDataArray removeAllObjects];
            }else{
                [compareDataArray addObject:dictionary];
            }
        }else{
            for (int i = 0; i < compareDataArray.count; i ++) {
                if ([compareDataArray[i][@"dataSource"][0] isEqualToString:dictionary[@"dataSource"][0] ]) {
                    [compareDataArray removeObjectAtIndex:i];
                }
            }
        }

    };

      [self.scrollView addSubview:Glodview2];
      [self.scrollView addSubview:Glodview1];
    
    


}

#pragma mark -- getters

- (ButtonAndTableView *)HotProductsButton{
    if (!_HotProductsButton) {
        _HotProductsButton = ({
            
            
            ButtonAndTableView *buttonAndTableView  = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(0, 0,frameWidth *0.7  , frameHeight * 0.05) dataSource:self.HotSellDic backImageName:@"理财_下拉框" selectImageName:@"理财_下拉框"];
            
            buttonAndTableView;
            
            
            
        });
        _HotProductsButton.delegate = self;
    }
    return _HotProductsButton;
}
- (UIButton *)checkButton{
    if (!_checkButton) {
        _checkButton = [[UIButton alloc]initWithFrame:CGRectMake(frameWidth  - frameWidth * 0.15,0, frameWidth * 0.15, frameHeight * 0.05)];
        [_checkButton setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_查看对比"] forState:UIControlStateNormal];
        [_checkButton addTarget:self action:@selector(cheakButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _checkButton;
}
- (NSDictionary *)HotSellDic{
    if (!_HotSellDic) {
        _HotSellDic =[NSDictionary dictionary];
    }
    return _HotSellDic;
}

@end
